package com.lovo;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class HomeworkDDZ extends JFrame {
    private List<Card> pukes = new ArrayList<>();
    public HomeworkDDZ(){
        this.setLayout(null);

        init();
        xipai();
        fapai();

        this.setSize(1000,800);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    // 初始化54张扑克牌
    public void init(){
//        String [] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
//        String [] types = "♠,♥,♣,♦".split(",");
        for(int i = 3;i <= 15;i++){
            for(int j = 1;j <= 4;j++){
                pukes.add(new Card(i,j));
            }
        }
        pukes.add(new Card(17,1));
        pukes.add(new Card(16,1));


    }
    // 洗牌
    public void xipai(){
        Collections.shuffle(pukes);
        System.out.println(pukes);
    }
    // 发牌
    public void fapai(){
        List<Card> player1 = pukes.subList(0,17);
        List<Card> player2 = pukes.subList(17,34);
        List<Card> player3 = pukes.subList(34,51);
        List<Card> base = pukes.subList(51,54);

        sort(player1);
        sort(player2);
        sort(player3);
        sort(base);

        for(int i = 0;i < player1.size();i++){
            JLabel label = new JLabel(player1.get(i).getImage());
            label.setBounds(20,20 + (player1.size() - i) * 30,105,150);
            this.add(label);
        }
        for(int i = 0;i < player2.size();i++){
            JLabel label = new JLabel(player2.get(i).getImage());
            label.setBounds(120 + (player2.size() - i) * 30,550,105,150);
            this.add(label);
        }

    }
    public void sort(List list){
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if(o1.getPointValue() == o2.getPointValue()){
                    return o1.getTypeValue() - o2.getTypeValue();
                }else{
                    return o1.getPointValue() - o2.getPointValue();
                }
            }
        });
    }

    public static void main(String[] args) {
        HomeworkDDZ ddz = new HomeworkDDZ();

    }
}
